package com.compass.doctor.ui.mine.viewmodel

import androidx.lifecycle.MutableLiveData
import com.compass.framework.ext.stringListToJson
import com.compass.framework.ext.toJson
import com.compass.framework.model.AuthInfo
import com.compass.framework.model.DataStatistics
import com.compass.framework.model.DoctorCode
import com.compass.framework.model.Evaluate
import com.compass.framework.model.EvaluationClassification
import com.compass.framework.model.HospitalizedLog
import com.compass.framework.model.InspectionRecord
import com.compass.framework.model.KnowledgeCenter
import com.compass.framework.model.PatientFollow
import com.compass.framework.model.PatientISeeRecord
import com.compass.framework.model.PrescriptionStatusWarp
import com.compass.framework.model.UsefulExpressions
import com.compass.framework.model.UsefulExpressionsCate
import com.compass.framework.model.User
import com.compass.framework.toast.TipsToast
import com.compass.framework.utils.LogUtil
import com.compass.network.callback.IApiErrorCallback
import com.compass.network.manager.ApiManager
import com.compass.network.viewmodel.BaseViewModel
import com.google.gson.JsonObject
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import java.io.File

/**
 * Description： 我的ViewModel
 * Date:2023/11/16 13:43
 */
class MineViewModel : BaseViewModel() {
    val followLiveData: MutableLiveData<MutableList<PatientFollow>?> = MutableLiveData()
    val rxStatusLiveData: MutableLiveData<PrescriptionStatusWarp?> = MutableLiveData()
    val updateLiveData: MutableLiveData<String?> = MutableLiveData()
    val updateExamineLiveData: MutableLiveData<Any?> = MutableLiveData()
    val userExamineLiveData:MutableLiveData<AuthInfo> = MutableLiveData()
    val updateInfoLiveData: MutableLiveData<User?> = MutableLiveData()
    val classLiveData = MutableLiveData<EvaluationClassification?>()
    val statisticsLiveData = MutableLiveData<DataStatistics?>()
    val recordLiveData: MutableLiveData<MutableList<PatientISeeRecord>?> = MutableLiveData()
    val evaluateLiveData: MutableLiveData<MutableList<Evaluate>?> = MutableLiveData()
    val homeVideoArticleLiveData: MutableLiveData<KnowledgeCenter?> = MutableLiveData()
    val hospitalizedLogLiveData: MutableLiveData<MutableList<HospitalizedLog>?> = MutableLiveData()
    val hospitalizedDetailsLiveData: MutableLiveData<HospitalizedLog?> = MutableLiveData()
    val inspectionRecordLiveData: MutableLiveData<MutableList<InspectionRecord>?> =MutableLiveData()


    fun getHomeVideoAndArticle() {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getHomeVideoAndArticle()
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                homeVideoArticleLiveData.value = null
            }
        }, successBlock = {
            if (it == null) {
                homeVideoArticleLiveData.value = null
            } else {
                homeVideoArticleLiveData.value = it
            }
        })
    }


    fun getPatientFollowList(count: Int, pageSize: Int = 10) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getPatientFollowList(count, pageSize)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                followLiveData.value = null
            }
        }, successBlock = {
            if (it == null || it.list.isEmpty()) {
                followLiveData.value = null
            } else {
                followLiveData.value = it.list
            }
        })
    }


    /**
     * 获取住院证详情
     */
    fun getHospitalizedDetails(id: String) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getHospitalizedDetails(id)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                hospitalizedDetailsLiveData.value = null
            }
        }, successBlock = {
            hospitalizedDetailsLiveData.value=it
        })
    }


    fun getHospitalizedLog(count: Int, pageSize: Int = 10) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getHospitalizedLog(count, pageSize)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                hospitalizedLogLiveData.value = null
            }
        }, successBlock = {
            if (it == null || it.list.isEmpty()) {
                hospitalizedLogLiveData.value = null
            } else {
                hospitalizedLogLiveData.value = it.list
            }
        })
    }


    fun getEvaluationClassification() {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getEvaluationClassification()
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                classLiveData.value = null
            }
        }, successBlock = {
            LogUtil.e("message==${it?.toJson()}")
            classLiveData.value = it
        })
    }


    fun getEvaluationList(pageNo: Int, satisfactionDegree: ArrayList<String>?) {
        val params = JsonObject().apply {
            addProperty("pageNo", pageNo)
            addProperty("pageSize", 10)
            add("satisfactionDegreeList", stringListToJson(satisfactionDegree))
        }
        launchUIWithResult(responseBlock = {
            ApiManager.api.getEvaluationList(params)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                evaluateLiveData.value = null
            }
        }, successBlock = {
            if (it == null || it.list.isNullOrEmpty()) {
                evaluateLiveData.value = null
            } else {
                evaluateLiveData.value = it.list
            }
        })
    }

    fun updateUserInfo(avatar: String?,meritIllness:String?,meritTherapeutic:String? ,introduce:String?,alternativeMobile:String?) {
        val params = JsonObject().apply {
            addProperty("avatar", avatar)
            addProperty("meritIllness", meritIllness)
            addProperty("meritTherapeutic", meritTherapeutic)
            addProperty("introduce", introduce)
            addProperty("alternativeMobile", alternativeMobile)

        }
        launchUIWithResult(responseBlock = {
            ApiManager.api.updateUserInfo(params)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                updateInfoLiveData.value = null
            }
        }, successBlock = {
            updateInfoLiveData.value=it
        })
    }

    /**
     * 查询医生认证信息
     */
    fun getDoctorAuthInfo(){
        launchUIWithResult(responseBlock = {
            ApiManager.api.getExamineInfo()
        }, errorCall = object :IApiErrorCallback{
            override fun onError(code: Int?, error: String?) {
                super.onError(code, error)
                TipsToast.showTips(error)
                userExamineLiveData.value = null
            }
        }, successBlock = {
            if(it!=null){
                userExamineLiveData.value = it
            }
        })
    }

    fun updateExamineInfo(doctorId:String?,doctorName: String?,doctorSex:String?,idCardPositive:String? ,idCardReverse:String?,licensedPositive:String?,licensedReverse:String?) {
        val params = JsonObject().apply {
            addProperty("doctorId", doctorId)
            addProperty("doctorName", doctorName)
            addProperty("doctorSex", doctorSex)
            addProperty("idCardPositive", idCardPositive)
            addProperty("idCardReverse", idCardReverse)
            addProperty("licensedPositive", licensedPositive)
            addProperty("licensedReverse", licensedReverse)

        }
        launchUIWithResult(responseBlock = {
            ApiManager.api.updateExamineInfo(params)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                updateExamineLiveData.value=null
            }
        }, successBlock = {
            updateExamineLiveData.value=it
        })
    }


    fun getPrescriptionRecordStatus() {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getPrescriptionRecordStatus()
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                super.onError(code, error)
                rxStatusLiveData.value = null
            }
        }, successBlock = {
            rxStatusLiveData.value = it
        })
    }


    fun getConsultationList(count: Int, pageSize: Int = 10) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getConsultationList(count, pageSize)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                recordLiveData.value = null
            }
        }, successBlock = {
            if (it == null || it.list.isEmpty()) {
                recordLiveData.value = null
            } else {
                recordLiveData.value = it.list
            }
        })
    }


    fun getDoctorInspectionList(count: Int, pageSize: Int = 10, type: Int) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getDoctorInspectionList(count, pageSize, type)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                inspectionRecordLiveData.value = null
            }
        }, successBlock = {
            if (it == null || it.list.isEmpty()) {
                inspectionRecordLiveData.value=null
            } else {
                inspectionRecordLiveData.value = it.list
            }

        })
    }


    val codeLiveData = MutableLiveData<DoctorCode?>()

    /**
     *获取医生小程序码
     * @param version 正式版	release 体验版	trial  开发版	develop
     */
    fun getHomePageQRCode(doctorId: String, version: String) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getHomePageQRCode(doctorId, version)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            codeLiveData.value = it
        })

    }


    /**
    数据统计
     */
    fun getDataStatistics(startTime:Long?,endTime:Long?) {

        val params = JsonObject().apply {
            addProperty("startTime", startTime)
            addProperty("endTime", endTime)
        }
        launchUIWithResult(responseBlock = {
            ApiManager.api.getDataStatistics(params)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                statisticsLiveData.value=null
            }
        }, successBlock = {
            statisticsLiveData.value=it
        })

    }



    fun uploadCA(file: File) {
        //File转RequestBody
        val fileBody = file.asRequestBody("image/jpg".toMediaType())
        //将文件转化为MultipartBody.Part
        //第一个参数：上传文件的key；第二个参数：文件名；第三个参数：RequestBody对象
        val filePart = MultipartBody.Part.createFormData("file", file.name, fileBody)
        launchUIWithResult(responseBlock = {
            ApiManager.api.uploadCA(filePart)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                super.onError(code, error)
                updateLiveData.value=null
            }
        }, successBlock = {
            it?.let {
                updateLiveData.value=it
            }
        })
    }
}